<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="落地页名称" prop="name"  label-width="98px">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入落地页名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="pName">
        <el-input
          v-model="queryParams.pName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="openTemplate"
          v-hasPermi="['recommd:luodiye:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['recommd:luodiye:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['recommd:luodiye:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['recommd:luodiye:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="luodiyeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="落地页名称" align="center" prop="name" />
      <el-table-column label="产品名称" align="center" prop="pName" />
      <el-table-column label="落地页链接" align="center" prop="url" >
        <template #default="scope">
          <span>{{ scope.row.url+"/"+scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['recommd:luodiye:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['recommd:luodiye:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改落地页对话框 -->
    <el-dialog :title="title" v-model="open" width="790px" append-to-body>
      <el-form ref="luodiyeRef" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="落地页名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入落地页名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品名称">
              <el-input v-model="form.pName" placeholder="请输入产品名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
              <el-form-item label="业务名称" prop="businessName">
                <el-input v-model="form.businessName" placeholder="请输入业务名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="资费标准" prop="businessFee">
                <el-input v-model="form.businessFee" placeholder="请输入资费标准" />
              </el-form-item>
            </el-col>
          </el-row>
        <el-row>
<!--          <el-col :span="8">
            <el-form-item label="选择模版" prop="merchandiseType">
              <el-select v-model="form.url" placeholder="请选择模版" style="width: 400px">
                <el-option
                    v-for="item in templateOptionsList"
                    :key="item.roleId"
                    :label="item.name"
                    :value="item.url">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>-->
          <el-col :span="12">
            <el-form-item label="渠道名称">
              <el-select v-model="form.channelId" placeholder="请输入渠道ID" style="width: 400px">
                <el-option
                    v-for="item in channelOptions"
                    :key="item.channelId"
                    :label="item.name"
                    :value="item.channelId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
         <el-col :span="12">
            <el-form-item label="产品ID" >
              <el-select v-model="form.pId" placeholder="请输入产品ID" style="width: 400px">
                <el-option v-for="item in listProductOptions" :key="item.id" :label="item.pName.slice(3,item.pName.length-4)" :value="item.pCode">
                  <!--                  {{ item.pName.slice(3) }}-->
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="banner首图" prop="bannerImg">
              <image-upload v-model="form.bannerImg" :limit="1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="交互框底图" prop="bottomImg">
              <image-upload v-model="form.bottomImg" :limit="1"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="办理按钮" prop="buttonImg">
              <image-upload v-model="form.buttonImg" :limit="1"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="用户协议" prop="xieyi">
          <el-input v-model="form.xieyi" placeholder="请输入用户协议"  />
        </el-form-item>
        <el-row>
          <el-col :span="9">
            <el-form-item label="协议一" prop="xieyiStatus1" label-width="90px">
              <el-radio-group v-model="form.xieyiStatus1">
                <el-radio label="M">展示</el-radio>
                <el-radio label="C">不展示</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item prop="xieyi1" label-width="1px">
              <el-input v-model="form.xieyi1" placeholder="请输入协议名称" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item prop="xieyiPic1" label-width="5px">
              <image-upload v-model="form.xieyiPic1" :limit="1"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="9">
            <el-form-item label="协议二" prop="xieyiStatus2" label-width="90px">
              <el-radio-group v-model="form.xieyiStatus2">
                <el-radio label="M">展示</el-radio>
                <el-radio label="C">不展示</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item prop="xieyi2" label-width="1px">
              <el-input v-model="form.xieyi2" placeholder="请输入协议名称" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item prop="xieyiPic2" label-width="5px">
              <image-upload v-model="form.xieyiPic2" :limit="1"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="用户协议">
          <el-input v-model="form.xieyi3" placeholder="等协议"  />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="协议未选中按钮" prop="xieyiCheck" label-width="110px">
              <image-upload v-model="form.xieyiCheck" :limit="1"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="协议选中按钮" prop="xieyiChecked" label-width="110px">
              <image-upload v-model="form.xieyiChecked" :limit="1"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="资费信息图" prop="feeImg" label-width="110px">
          <image-upload v-model="form.feeImg" :limit="1"/>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="业务说明图" prop="descImg" label-width="110px">
          <image-upload v-model="form.descImg" :limit="1"/>
        </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="title" v-model="flag" width="1120px" append-to-body>
      <div class="image-container">
        <div v-for="(image, index) in templateOptionsList" :key="index" class="image-item">


          <image-preview :src="image.pic" :width="320" :height="400"/>
          <div class="check">
            <label class="checkbox-label">
              <input
                  :id="'id-' + image.id"
                  type="checkbox"
                  :value="image.id"
                  @change="(event) => handleTechChange(event, image.id, {id:image.id,name:image.name,url: image.url})"
                  class="checkbox-input"
              ><!--{{form}}-->
            </label>
          </div>
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleAdd">确 定</el-button>
          <el-button @click="cancelTemplate">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Luodiye">
import { listLuodiye, getLuodiye, delLuodiye, addLuodiye, updateLuodiye } from "@/api/recommd/luodiye";
import {listTemplate} from "@/api/recommd/template.js";
import { listProduct} from "@/api/merchandise/product";
const { proxy } = getCurrentInstance();
const checked1 = ref(false)
const selectedTech = ref([])
const luodiyeList = ref([]);
const open = ref(false);
const flag = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const templateOptionsList = ref([]);
const listProductOptions = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    pName: null,
    pId: null,
    channelId: null,
    bussinessName: null,
    bussinessFee: null,
    url: null,
    bannerImg: null,
    bottomImg: null,
    buttonImg: null,
    checkImg: null,
    checkedImg: null,
    feeImg: null,
    descImg: null,
    xieyiStatus1: null,
    xieyiStatus2: null,
    xieyi1: null,
    xieyi2: null,
    xieyiPic1: null,
    xieyiPic2: null,
    xieyiCheck: null,
    xieyiChecked: null,
    xieyi: null
  },
  rules: {
  },
  templateOption: {
    name: null,
    id: null,
    url: null,
  },
  channelOptions: [{
    channelId: '87596a9058',
    name: '知护星渠道号'
  }, {
    channelId: '99086fa3e0',
    name: '湛杰渠道代码'
  }],
});

const { queryParams, form, rules,channelOptions,templateOption } = toRefs(data);

const handleTechChange = (event, item, o) => {
  console.log('选中的技术:', event.target,item,o)
  let check = event.target.checked;
  if(check){
    let checkbox = document.getElementById("id-"+templateOption.value.id);
    if(checkbox){
      checkbox.checked = false;
    }
    templateOption.value={name: null, id: null, url: null,}

    templateOption.value={name: o.name, id: o.id, url: o.url,}
    form.value.url = o.url
    form.value.check = check
  }else {
    templateOption.value={name: null, id: null, url: null,}
    form.value.url = null
    form.value.check = check
  }
}
/** 查询落地页列表 */
function getList() {
  loading.value = true;
  listLuodiye(queryParams.value).then(response => {
    luodiyeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function getTeplateList() {
  queryParams.value.pageSize=1000;
  loading.value = true;
  listTemplate(queryParams.value).then(response => {
    templateOptionsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function getListProduct(){
  queryParams.value.pageSize=1000;
  listProduct(queryParams.value).then(response => {
    listProductOptions.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/*/!** 新增按钮操作 *!/
function handleAdd() {
  reset();
  getUser().then(response => {
    postOptions.value = response.posts;
    roleOptions.value = response.roles;
    open.value = true;
    title.value = "添加用户";
    form.value.password = initPassword.value;
  });
};*/
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
function cancelTemplate() {
  flag.value = false;
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    pName: null,
    url: null,
    pId: null,
    channelId: null,
    createTime: null,
    updateTime: null,
    bannerImg: null,
    bottomImg: null,
    buttonImg: null,
    checkImg: null,
    checkedImg: null,
    feeImg: null,
    descImg: null,
    xieyiStatus1: null,
    xieyiStatus2: null,
    xieyi1: null,
    xieyi2: null,
    xieyiPic1: null,
    xieyiPic2: null,
    xieyiCheck: null,
    xieyiChecked: null,
    xieyi: null
  };
  proxy.resetForm("luodiyeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function openTemplate() {
  reset();
  flag.value = true;
  title.value = "选择模版";
}
/** 新增按钮操作 */
function handleAdd() {
  flag.value = false
  open.value = true;
  form.value.xieyi = '我已阅读并同意'
  form.value.xieyi1 = '活动规则'
  form.value.xieyi2 = '隐私条款'
  title.value = "添加落地页";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getLuodiye(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改落地页";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["luodiyeRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateLuodiye(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addLuodiye(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除落地页编号为"' + _ids + '"的数据项？').then(function() {
    return delLuodiye(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('recommd/luodiye/export', {
    ...queryParams.value
  }, `luodiye_${new Date().getTime()}.xlsx`)
}

getList();
getTeplateList();
getListProduct();
</script>
<style>
.image-container{
  display: flex;
  flex-wrap: wrap; /* 关键属性：允许换行 */
  gap: 40px; /* 项目间距 */
  padding: 20px;
}
.image-item{
  text-align: center;
}
.dialog-footer{
  text-align: center;
}
.check{
  padding-top: 10px;

}
.checkbox-input{
  width: 20px;
  height: 20px;
}
.el-dialog{
  //background-color: rgba(0, 0, 0, .4);
}
</style>